<template>
	<view class="">
		<view :style="{height: titleNavHeight + statusBarHeight + 5 + 'px'}"
			style="position: fixed;top: 0;left: 0;right: 0;z-index: 9999;background-color: #ffffff;
			border-bottom: 1px solid #ebedf0;"
		>
			<view :style="{height: titleNavHeight + 'px', top: statusBarHeight + 'px', width: '100%'}"
				class="nav-bar"
			>
				<text class="text">消息</text>
			</view>
		</view>
		<view :style="{height: titleNavHeight + statusBarHeight + 5 + 'px'}"></view>
		<view>
			<u-tabs :list="tabsList" :is-scroll="false" :current="current" @change="change" :active-color="theme.primary_color"></u-tabs>
			<scroll-view :scroll-top="0" scroll-y="true" class="scroll-Y"
				:style="{height: 'calc(100vh - ' + (titleNavHeight + statusBarHeight + 130) + 'px)'}"
				:refresher-triggered="triggered" refresher-enabled @refresherrefresh="refresh"
				@scrolltolower="tolower"
			>
				<view style="width: 100%;height: 100%;" v-if="current == 0">
					<view class="messagecard" v-for="(data,index) in RemindingMessageList" :key="index">
						<MessageCard title="催办消息" :time="data.timeData" :cardImg="pressIcon"
							:message="data.createUsername + '提醒您办理' + data.taskName"
							@click.native="onHanlderTask(data)">
						</MessageCard>
					</view>
					<u-empty style="margin: auto 0;" text="暂无数据" mode="list" v-if="RemindingMessageList.length <= 0"></u-empty>
				</view>
				<view style="width: 100%;height: 100%;" v-if="current == 1">
					<view class="messagecard" v-for="(data, index) in CopyMessageList" :key="index" >
						<MessageCard title="抄送消息" :time="data.timeData" :cardImg="copyIcon"
							:message="data.createUsername + '抄送给您' + data.taskName"
							@click.native="onHanlderTask(data,data.messageId)">
						</MessageCard>
					</view>
					<u-empty style="margin: auto 0;" text="暂无数据" mode="list" v-if="CopyMessageList.length <= 0"></u-empty>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {getBoundingClientRect} from "@/utils";
	import MessageCard from './messageCard.vue';
	import { FlowOperationController } from '@/api/flowController.js';

	export default{
		components: {
			MessageCard
		},
		data(){
			const {
				windowWidth,
				statusBarHeight
			} = uni.getSystemInfoSync()
      const rect = getBoundingClientRect(windowWidth, statusBarHeight);
			return {
				windowWidth: windowWidth,
				statusBarHeight: statusBarHeight,
				titleNavHeight: (rect.top - statusBarHeight) * 2 + rect.height,
				emptyWidth: rect.left - 10,
				headerHeight:0,
				current: 0,
				pageNum: 1,
				pressIcon: require('@/static/icon/messagePressIcon2x.png'),
				copyIcon: require('@/static/icon/messageCopyIcon2x.png'),
				tabsList: [
					{
						name:'催办消息'
					},
					{
						name:'抄送消息'
					}
				],
				RemindingMessageList:[],
				RemindingMessageTotalCount: '',
				CopyMessageList:[],
				CopyMessageTotalCount:'',
				triggered:false,
			}
		},
		onShow() {
			this.pageNum = 1
			this.loadRemindingMessage(this.pageNum)
		},
		methods:{
			refresh(){
				this.triggered = true
				this.pageNum = 1
				if(this.current == 0){
					this.loadRemindingMessage(this.pageNum)
				}else{
					this.loadCopyMessage (this.pageNum)
				}
			},
			tolower(){
				this.pageNum++
				if(this.current == 0){
					this.loadRemindingMessage(this.pageNum)
				}else{
					this.loadCopyMessage (this.pageNum)
				}
			},
			change(index){
				this.current = index
				if(index == 0) {
					this.loadRemindingMessage(this.pageNum)
				} else if(index == 1) {
					this.loadCopyMessage(this.pageNum)
				}
			},
			loadRemindingMessage (pageNum) {
				let params = {
					pageParam: {
						pageNum: pageNum,
						pageSize: 10,
						count: false
					}
				}
				FlowOperationController.listRemindingTask(this, params).then(res => {
					let dataList = res.dataList.map(item => {
						var time = new Date(item.createTime)
						let year = time.getFullYear();
						let month = (time.getMonth() + 1).toString();
						let data = time.getDate().toString();
						let times = year + '/' + month + '/' + data
						return {
							...item,
							timeData: times
						}
					})
					if (pageNum) {
						this.RemindingMessageList = dataList
					} else {
						this.RemindingMessageList = this.RemindingMessageList.concat(dataList)
					}
					this.RemindingMessageTotalCount = res.totalCount
					this.triggered = false
				}).catch(e => {
					console.log(e)
				});
			},
			loadCopyMessage (pageNum) {
				let params = {
					pageParam: {
						pageNum: pageNum,
						pageSize: 10,
						count: false
					}
				}
				FlowOperationController.listCopyMessage(this, params).then(res => {
					let dataList = res.dataList.map(item => {
						var time = new Date(item.createTime)
						let year = time.getFullYear();
						let month = (time.getMonth() + 1).toString();
						let data = time.getDate().toString();
						let times = year + '/' + month + '/' + data
						return {
							...item,
							timeData: times
						}
					})
					if (pageNum === 1) {
						this.CopyMessageList = dataList
					} else {
						this.CopyMessageList = this.CopyMessageList.concat(dataList)
					}
					this.CopyMessageTotalCount = res.totalCount
				}).catch(e => {
					console.log(e)
				});
			},
			onHanlderTask (row, messageId){
				console.log(row)
				let isCopy = messageId != null;
				let params = {
					processInstanceId: row.processInstanceId,
					processDefinitionId: row.processDefinitionId,
					taskId: row.taskId
				}
				let httpCall = isCopy ? FlowOperationController.viewInitialHistoricTaskInfo(this, params) : FlowOperationController.viewRuntimeTaskInfo(this, params);
				httpCall.then(res => {
						const data = {
							isRuntime: !isCopy,
							taskId: row.taskId,
							messageId: messageId,
							processDefinitionKey: row.processDefinitionKey,
							processInstanceId: row.processInstanceId,
							processDefinitionId: row.processDefinitionId,
							formId: res.formId,
							routerName: res.mobileRouterName || res.routerName,
							readOnly: isCopy ? true : res.readOnly,
							taskName: row.taskName,
							flowEntryName: row.processDefinitionName,
							processInstanceInitiator: row.processInstanceInitiator,
							// 过滤掉加签和撤销操作，加签只有在已完成任务里可以操作
							operationList: (res.operationList || []).filter(item => {
								return item.type !== this.SysFlowTaskOperationType.CO_SIGN && item.type !== this.SysFlowTaskOperationType.REVOKE && item.type !== this.SysFlowTaskOperationType.SIGN_REDUCTION;
							}),
							variableList: res.variableList
						}
						uni.navigateTo({
							url: '/views/workflow/handleFlowTask/index?passData=' + encodeURIComponent(JSON
								.stringify(
									data))
						})
				}).catch(e => {});

			}
		}
	}
</script>

<style scoped lang="less">
	.nav-bar {
			position: absolute;
			display: flex;
			flex-direction: row;
			align-items: center;
			padding-left: 20rpx;

			.text {
				max-width: 60%;
				margin: 0 auto;
				color: #323233;
				font-weight: 500;
				font-size: 30rpx;
			}

			.nav-right {
				display: flex;

				.mobile-font {
					color: #707070;
					font-size: 36rpx;

					&+.mobile-font {
						margin-left: 12rpx;
					}
				}
			}
		}
	.scroll-Y{
		padding-top: 32rpx;
	}
	.messagecard{
		padding: 0 32rpx;
		margin-bottom: 32rpx;
	}
</style>